package com.ebupt.dbmodule

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.DifferCallback
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.ebupt.dbmodule.databinding.ItemUserBinding
import com.ebupt.service.AppUserInfo

class AppUserAdapter:PagingDataAdapter<AppUserInfo,UserViewHolder>(differCallback) {

    companion object{
       private val differCallback = object :DiffUtil.ItemCallback<AppUserInfo>(){

           override fun areItemsTheSame(oldItem: AppUserInfo, newItem: AppUserInfo): Boolean {
              return oldItem.id==newItem.id
           }

           override fun areContentsTheSame(oldItem: AppUserInfo, newItem: AppUserInfo): Boolean {
              return oldItem.toString() == newItem.toString()
           }

       }
    }

    override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
        getItem(position)?.let {
            holder.bind(it)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder = UserViewHolder.createUserViewHolder(parent)

}

class UserViewHolder(val itembinding:ItemUserBinding):RecyclerView.ViewHolder(itembinding.root){

    companion object{
        fun createUserViewHolder(parent:ViewGroup):UserViewHolder{
            return UserViewHolder(ItemUserBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent, false
            ))
        }
    }

    fun bind(userinfo:AppUserInfo){
        itembinding.userinfo = userinfo
    }
}